package cn.sumpu.app.guoguojie.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtil {
	
	
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd");

	private final static Logger logger = LoggerFactory
			.getLogger(DateUtil.class);

	public static String getToday(Date time){
		SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
		return s.format(time);
	}
	public static String getYesterday(){
		SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
		return s.format(new Date(new Date().getTime() - 1000*60*60*24));
	}
	
	public static String getAge(String birthday) {
		if(Common.emptyStr(birthday) || birthday.length()<10) {
			return "0"; 
		}
		try {
			Date birthdayDate = sdf.parse(birthday);
			Calendar cal = Calendar.getInstance(Locale.CHINA);

			if (cal.before(birthdayDate)) {
				throw new IllegalArgumentException(
						"The birthDay is before Now.It's unbelievable!");
			}

			int yearNow = cal.get(Calendar.YEAR);
			int monthNow = cal.get(Calendar.MONTH);
			int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
			
			
			cal.setTime(birthdayDate);
			int yearBirth = cal.get(Calendar.YEAR);
			int monthBirth = cal.get(Calendar.MONTH);
			int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
			int ageYear = yearNow - yearBirth;
			int ageMonth=0;
			if (monthNow <= monthBirth) {
				if (monthNow == monthBirth) {
					// monthNow==monthBirth
					if (dayOfMonthNow < dayOfMonthBirth) {
						ageYear--;
						ageMonth=12-monthBirth+monthNow-1;
					} else {
						//ageYear
					}
				} else {
					// monthNow<monthBirth
					ageYear--;
					ageMonth=12-monthBirth+monthNow;
				}
			} else {
				// monthNow>monthBirth
				ageMonth=monthNow-monthBirth;
			}
			 String[] str=new String[2];
			 str[0] = String.valueOf(ageYear);
		     str[1] = String.valueOf(ageMonth);
		     String allStr2 = new String();
		     if(ageYear<0){
		    	 allStr2="0岁";
		     }
		     else if(ageYear==0){
		            allStr2 = str[1]+"个月";
		     }
		     else
		     {
		    	    allStr2 = str[0]+"岁" + str[1]+"个月";
		     }
			return allStr2;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Parse birthday failure![birthday: "+birthday+"]", e);
			}
			return "0";
		}
	}
	
	//根据年份取得年龄
	public static String getAgeByYear(String year){
		try{
			Calendar now=Calendar.getInstance();
			Integer nowYear = now.get(Calendar.YEAR);
			int age = nowYear - Integer.parseInt(year);
			if(age==0){
				return "今年出生";
			}
			return String.valueOf(age+"岁");
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}

	//将字符串转化为时间类型
	public static Date parseDate(String dateStr){
		try{
			if(Common.emptyStr(dateStr)){
				return null;
			}
			String pattern = "yyyy-MM-dd";
			if(dateStr.length()>11){
				pattern = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			Date date = sdf.parse(dateStr);
			return date;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static Date getBirthdayDate(String birthday) {
		Date birthdayDate;
		try {
			
			birthdayDate = sdf.parse(birthday);
			return birthdayDate;
			
		} catch (ParseException e) {
			
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	//得到宝宝年龄的周数
	public static String getAgeWeek(String birthday) {
		if(Common.empty(birthday)) return "0"; 
		try {
			Date birthdayDate = sdf.parse(birthday);
			Calendar cal = Calendar.getInstance(Locale.CHINA);

			if (cal.before(birthdayDate)) {
				throw new IllegalArgumentException(
						"The birthDay is before Now.It's unbelievable!");
			}

			int yearNow = cal.get(Calendar.YEAR);
			int monthNow = cal.get(Calendar.MONTH);
			int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
			
			
			cal.setTime(birthdayDate);
			int yearBirth = cal.get(Calendar.YEAR);
			int monthBirth = cal.get(Calendar.MONTH);
			int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
			int ageYear = yearNow - yearBirth;
			int ageMonth=0;
			int ageWeek=0;
			if (monthNow <= monthBirth) {
				if (monthNow == monthBirth) {
					// monthNow==monthBirth
					if (dayOfMonthNow < dayOfMonthBirth) {
						ageYear--;
						ageMonth=12-monthBirth+monthNow-1;
						ageWeek=dayOfMonthNow/7;
					} else {
						//dayOfMonthNow >= dayOfMonthBirth
						ageWeek=(dayOfMonthNow-dayOfMonthBirth)/7;
					}
				} else {
					// monthNow<monthBirth
					if(dayOfMonthNow < dayOfMonthBirth){
					ageYear--;
					ageMonth=12-monthBirth+monthNow-1;
					ageWeek=(30-(dayOfMonthBirth-dayOfMonthNow))/7;
					}
					else{
						//dayOfMonthNow >= dayOfMonthBirth
						ageYear--;
						ageMonth=12-monthBirth+monthNow-1;
						ageWeek=(dayOfMonthNow-dayOfMonthBirth)/7;
					}
					
				}
			} else {
				// monthNow>monthBirth
				if(dayOfMonthNow < dayOfMonthBirth){
					
				ageMonth=monthNow-monthBirth-1;
				ageWeek=(30-(dayOfMonthBirth-dayOfMonthNow))/7;
				}
				
				else{//dayOfMonthNow >= dayOfMonthBirth
					ageMonth=monthNow-monthBirth;
					ageWeek=(dayOfMonthNow-dayOfMonthBirth)/7;
					
				}
				
			}
			 String[] str=new String[3];
			 str[0] = String.valueOf(ageYear);
		     str[1] = String.valueOf(ageMonth);
		     str[2] = String.valueOf(ageWeek);
		     String allStr2 = new String();
		     if(ageYear<0){
		    	 allStr2="0岁";
		     }
		     else if(ageYear==0){
		    	 if(ageMonth==0 && ageWeek==0 ){
		    		 allStr2 ="未满一周";
		            
		            }else{
		            	allStr2 = str[1]+"个月"+str[2]+"周";
		            }
		     }
		     else 
		     {
		    	    allStr2 = str[0]+"岁" + str[1]+"个月"+str[2]+"周";
		     }
			return allStr2;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Parse birthday failure!", e);
			}
			return "0";
		}
	}
	
	//比较今年日期相差天数
	public static int daysOfTwo(Date fDate, Date oDate) {
	       Calendar aCalendar = Calendar.getInstance();
	       aCalendar.setTime(fDate);
	       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
	       aCalendar.setTime(oDate);
	       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
	       return day2-day1;
	}
	public static void main(String[] args) {
		
	}
}
